<?php
/*
	代理中心
*/
defined('BY_JYA') or exit('error');
$_RQ['op']=$_RQ['op']?$_RQ['op']:'main';
if($_RQ['op']=='main'){
	$return['setting']=$this->plugin_setting('page');
	$member=$this->get('member',array('merchid'=>$_SESSION['onemerchid'],'mid'=>$_SESSION['uid']));
	$basic=model('core/member')->basic($_SESSION['uid']);
	$member=array_merge($member,$basic);
	$member['storeurl']=custom_url('jy_mshop2fee/myhome/main/mid='.$_SESSION['uid'].'&fromstore='.$_SESSION['uid'],true);
	$image=ROOT_AT.'oss/'.$_SESSION['uniacid'].'/images/jy_mshop2fee/fromstore'.$_SESSION['uid'].'.jpg';
	if(!file_exists($image)){
		$url='/jy_mshop2fee/myhome/index?mid='.$_SESSION['uid'].'&fromstore='.$_SESSION['uid'];
		$wxapp=pdo_get('core_account_wxapp',array('id'=>$_SESSION['wxapp_id'],'uniacid'=>$_SESSION['uniacid']));
		$res=model('core/wxapp')->appcode($url,$wxapp['appid'],$wxapp['appsecret'],200);
		$path=ROOT_AT.'oss/'.$_SESSION['uniacid'].'/images/jy_mshop2fee';
		if(!file_exists($path)){
			mkdir($path,0700, true);
		}
		file_put_contents($image,$res);
	}
	$return['appcode'] = 'oss/'.$_SESSION['uniacid'].'/images/jy_mshop2fee/fromstore'.$_SESSION['uid'].'.jpg';
	$return['member']=$member;
	$return['member']['levelname']=$this->getcolumn('level',array('id'=>$member['level_id']),'title');
	$return['member']['levelname']=$return['member']['levelname']?:$this->diylevelname;
	$data=array();
	//已结算佣金
	$data['balance_total']=$this->sum('balance',array('merchid'=>$_SESSION['onemerchid'],'mid'=>$_SESSION['uid']),'money');
	$data['balance_num']=$this->count('balance',array('merchid'=>$_SESSION['onemerchid'],'mid'=>$_SESSION['uid']));
	//待结算佣金
	$params=array('merchid'=>$_SESSION['onemerchid'],'uniacid'=>$_SESSION['uniacid'],'mid'=>$_SESSION['uid'],'status'=>1);
	/*if($this->setting['balance_day']){
		//结算天数
		$time=TIMESTAMP-intval($this->setting['balance_day'])*86400;
		$params['complete_time <']=$time;
	}*/
	$data['commission_1']=$this->sum('commission',$params,'money');
	$data['commission_total']=$this->sum('commission',array('merchid'=>$_SESSION['onemerchid'],'uniacid'=>$_SESSION['uniacid'],'mid'=>$_SESSION['uid']),'money');
	$data['dong_fee']=$this->sum('commission',array('merchid'=>$_SESSION['onemerchid'],'uniacid'=>$_SESSION['uniacid'],'mid'=>$_SESSION['uid'],'isjiedong'=>0),'dong_fee');
	$data['area_num']=$this->count('member',array('merchid'=>$_SESSION['onemerchid'],'uniacid'=>$_SESSION['uniacid'],'fromuser'=>$_SESSION['uid']));
	$params=array('merchid'=>$_SESSION['onemerchid'],'uniacid'=>$_SESSION['uniacid'],'mid'=>$_SESSION['uid']);
	$data['jindou_money']=$this->sum('commission',$params,'jifen');
	$params['type']=0;
	$data['zhi_money']=$this->sum('commission',$params,'money');
	$params['type']=1;
	$data['jian_money']=$this->sum('commission',$params,'money');
	$params['type']=2;
	$data['red_money']=$this->sum('commission',$params,'money');
	$params['type']=4;
	$data['ping_money']=$this->sum('commission',$params,'money');
	$params['type']=5;
	$data['pingfee_money']=$this->sum('commission',$params,'money');
	$order_num=$this->count('commission',array('merchid'=>$_SESSION['onemerchid'],'uniacid'=>$_SESSION['uniacid'],'mid'=>$_SESSION['uid']));
	$params=array('uniacid'=>$_SESSION['uniacid']);
	$orderby='level asc';
	$alllevels=$this->getall('level',$params,array(),'',$orderby);
	$levelnum=count($alllevels)+2;
	$agent_ids=$_SESSION['uid'];
	$agent_num=0;
	for($i=0;$i<$levelnum;$i++){
		$level=$i+1;
		if(!$agent_ids){
			$levels[$level]=0;
			break;
		}
		$agent_ids=$this->getall('member',array('merchid'=>$_SESSION['onemerchid'],'fromuser'=>$agent_ids),array('mid'),'mid');		
		if(!$agent_ids){
			$levels[$level]=0;
		}else{
			$agent_ids=array_keys($agent_ids);
			$order_num +=$this->count('commission',array('merchid'=>$_SESSION['onemerchid'],'uniacid'=>$_SESSION['uniacid'],'mid'=>$agent_ids));
			$levels[$level]=count($agent_ids);
			$agent_num +=$levels[$level];
		}
	}
	$data['agent_num']=$agent_num;
	$data['order_num']=$order_num;
	$data['fromstore_num']=$this->count('member',array('merchid'=>$_SESSION['onemerchid'],'uniacid'=>$_SESSION['uniacid'],'storemid'=>$_SESSION['uid']));
	$return['data']=$data;
	set_html('title',$return['setting']['center']?:'分销中心');
	exi($return);
}

?>